package timeDemo;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Calendar1 {
	public static void main(String[] args) throws ParseException {
		
		float m=1234.56f;
		DecimalFormat df = new DecimalFormat("#.00");
		        System.out.println(df.format(m));
		
		System.out.println(String.format("%.2f", m));
		
		System.out.println(big2(20171011.00f));
		System.out.println(big2(20171012.00f));
		System.out.println(big2(20171013.00f));
		System.out.println(big2(20171014.00f));
		System.out.println(big2(20171015.00f));
		System.out.println(big2(20171016.00f));
		System.out.println(big2(20171017.00f));
		System.out.println(big2(20171018.00f));
		System.out.println(big2(20171019.00f));
		System.out.println("======");
		System.out.println(big2(20171015.00f));
		System.err.println(Float.parseFloat("20171014.00d"));
		System.err.println(Float.parseFloat("20171015.00d"));
		System.out.println((float)20171014);
		System.out.println(String.valueOf((float)20171015));
//		Date date=null;
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
//		date = sdf.parse("2017-03-01");
//		Calendar c = Calendar.getInstance(); 
//		c.setTime(date); 
//		System.out.println(sdf.format(c.getTime()));//2010-04-24 
//		c.add(Calendar.DATE, 1); 
//		System.out.println(sdf.format(c.getTime()));//2010-04-25 
//		c.add(Calendar.DATE, -1); 
//		System.out.println(sdf.format(c.getTime()));//2010-04-25 
		
//		System.out.println(getBeforeToday(20171022f));
//		System.out.println(getBeforeToday(20171022.09d));
		
	}	
	
	//方法二： BigDecimal
    private static String big2(double d) {
        BigDecimal d1 = new BigDecimal(Double.toString(d));
        BigDecimal d2 = new BigDecimal(Integer.toString(1));
        // 四舍五入,保留2位小数
        return d1.divide(d2,2,BigDecimal.ROUND_DOWN).toString();
    }
	

	// 方法一：NumberFormat
	    private static String big(Float d) {
	        NumberFormat nf = NumberFormat.getInstance();
	        // 是否以逗号隔开, 默认true以逗号隔开,如[123,456,789.128]
	        nf.setGroupingUsed(false);
	        // 结果未做任何处理
	        return nf.format(d);
	    }

	  private static float getBeforeToday(float f) throws ParseException {
	        if (f<=0){
	            return f;
	        }
	        Date date=null;
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
	        String valueOf = String.valueOf((int)f);
	        System.err.println("int 型  "+valueOf);
	        date = sdf.parse(valueOf);
	        Calendar c = Calendar.getInstance();
	        c.setTime(date);
	        c.add(Calendar.DATE, -1);
	        String dayBefore = sdf.format(c.getTime());
	        System.out.println("前一天  str==="+dayBefore);
	        System.err.println("Float.parseFloat====="+Float.parseFloat(dayBefore));
	        BigDecimal bigDecimal = new BigDecimal(dayBefore);
	     
	        
	        System.out.println(bigDecimal.longValue()+"long");
	        System.out.println((int)bigDecimal.longValue()+"longTTT");
	        
	        double doubleValue = bigDecimal.floatValue();
	        System.out.println(doubleValue+"ddd");
	        bigDecimal.floatValue();
	        float parseFloat = Float.parseFloat(dayBefore);
////	        float parseFloat = bigDecimal.floatValue();
//	        double parseFloat = bigDecimal.doubleValue();
//	        
	        System.out.println(parseFloat+"www");
	        System.out.println(parseFloat-20171010+"运算1");//20171020
	        System.out.println(parseFloat-20171022+"运算2");
	        System.out.println(parseFloat-20171021+"运算3");
	        System.err.println((int) parseFloat+"----转换int");
	        return Float.parseFloat(sdf.format(c.getTime()));
	    }
	  
	  private static double getBeforeToday(double f) throws ParseException {
	        if (f<=0){
	            return f;
	        }
	        Date date=null;
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
	        String valueOf = String.valueOf((int)f);
	        System.err.println(valueOf);
	        date = sdf.parse(valueOf);
	        Calendar c = Calendar.getInstance();
	        c.setTime(date);
	        c.add(Calendar.DATE, -1);
	        double parseFloat = Double.parseDouble(sdf.format(c.getTime()));
	        System.err.println((int) parseFloat+"----");
	        return Double.parseDouble(sdf.format(c.getTime()));
	    }
}
